<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="短标题" prop="shortTitle">
        <el-input
          v-model="queryParams.shortTitle"
          placeholder="请输入短标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型id" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入类型id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否收费" prop="isCharge">
        <el-input
          v-model="queryParams.isCharge"
          placeholder="请输入是否收费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否公开" prop="isPublic">
        <el-input
          v-model="queryParams.isPublic"
          placeholder="请输入是否公开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createPerson">
        <el-input
          v-model="queryParams.createPerson"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updatePerson">
        <el-input
          v-model="queryParams.updatePerson"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:article:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <el-table-column label="短标题" align="center" prop="shortTitle" />
      <el-table-column label="类型id" align="center" prop="categoryId" />
      <el-table-column label="是否收费" align="center" prop="isCharge" />
      <el-table-column label="是否公开" align="center" prop="isPublic" />
      <el-table-column label="访问计数" align="center" prop="count" />
      <el-table-column label="点赞总数" align="center" prop="likesNum" />
      <el-table-column label="收藏总数" align="center" prop="favoritesNum" />
      <el-table-column label="审批" align="center" prop="isAudit">
        <template slot-scope="scope">
          <label class="switch" @change="clickAudit(scope.row)">
            <input type="checkbox" v-model="scope.row.isAudit">
            <span class="slider"></span>
          </label>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-audit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['system:article:audit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户头像" prop="userPhoto">
          <el-input v-model="form.userPhoto" placeholder="请输入用户头像" />
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="短标题" prop="shortTitle">
          <el-input v-model="form.shortTitle" placeholder="请输入短标题" />
        </el-form-item>
        <el-form-item label="文章图片" prop="articlePhoto">
          <el-input v-model="form.articlePhoto" placeholder="请输入文章图片" />
        </el-form-item>
        <el-form-item label="类型id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入类型id" />
        </el-form-item>
        <el-form-item label="是否收费" prop="isCharge">
          <el-input v-model="form.isCharge" placeholder="请输入是否收费：0-不收费，1-收费" />
        </el-form-item>
        <el-form-item label="是否公开" prop="isPublic">
          <el-input v-model="form.isPublic" placeholder="请输入是否公开：0-公开，1-不公开" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审批文章弹出层 -->
    <el-dialog :title="auditTitle" :visible="auditOpen" width="60vw" append-to-body @close="auditCancel">
      <el-form ref="article" :model="article" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-text >{{article.title}}</el-text>
        </el-form-item>
        <el-form-item label="短标题" prop="shortTitle">
          <el-text >{{article.shortTitle}}</el-text>
        </el-form-item>
        <el-form-item label="文章封面" prop="articlePhoto">
          <el-image :src="article.articlePhoto" style="width: 50%"></el-image>
<!--          <el-text >{{article.articlePhoto}}</el-text>-->
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-text >{{article.username}}</el-text>
        </el-form-item>
        <el-form-item label="文章内容" prop="username">
          <div v-html="article.content" style="padding-right: 60px"></div>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="auditCancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
  showAuditArticle,
  changeAuditState
} from "@/api/system/article";

export default {
  name: "Article",
  data() {
    return {
      // 审核回显的文章
      article: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章管理表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审批弹出层
      auditOpen: false,
      // 审批弹出层标题
      auditTitle: "文章详情",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        username: null,
        userPhoto: null,
        title: null,
        shortTitle: null,
        articlePhoto: null,
        categoryId: null,
        isCharge: null,
        isPublic: null,
        source: null,
        sourceUrl: null,
        count: null,
        likesNum: null,
        favoritesNum: null,
        createPerson: null,
        updatePerson: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        shortTitle: [
          { required: true, message: "短标题不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "类型id不能为空", trigger: "blur" }
        ],
        isCharge: [
          { required: true, message: "是否收费", trigger: "blur" }
        ],
        isPublic: [
          { required: true, message: "是否公开", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章管理列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 审批取消按钮
    auditCancel() {
      this.auditOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        username: null,
        userPhoto: null,
        title: null,
        shortTitle: null,
        articlePhoto: null,
        categoryId: null,
        isCharge: null,
        isPublic: null,
        source: null,
        sourceUrl: null,
        count: null,
        likesNum: null,
        favoritesNum: null,
        createTime: null,
        updateTime: null,
        createPerson: null,
        updatePerson: null
      };
      this.article = {
        id: null,
        userId: null,
        username: null,
        userPhoto: null,
        title: null,
        shortTitle: null,
        articlePhoto: null,
        categoryId: null,
        isCharge: null,
        isPublic: null,
        source: null,
        sourceUrl: null,
        count: null,
        likesNum: null,
        favoritesNum: null,
        createTime: null,
        updateTime: null,
        createPerson: null,
        updatePerson: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 审核按钮操作 */
    clickAudit(row) {
      const id = row.id || this.id;
      changeAuditState(id).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("修改成功");
        }
      })
    },

    /** 详情按钮操作 */
    handleAudit(row) {
      const id = row.id || this.id;
      this.auditOpen = true;
      showAuditArticle(id).then(response => {
        this.article = response.data;
        console.log("审批文章内容:"+this.article);
        this.auditTitle = "审批文章";
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章管理编号为"' + ids + '"的数据项？').then(function() {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
  /* From Uiverse.io by RaspberryBee */
  /* The switch - the box around the slider */
  .switch {
    font-size: 12px;
    position: relative;
    display: inline-block;
    width: 3.5em;
    height: 2em;
  }

  /* Hide default HTML checkbox */
  .switch input {
    opacity: 0;
    width: 0;
    height: 0;
  }

  /* The slider */
  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgb(182, 182, 182);
    transition: .4s;
    border-radius: 10px;
  }

  .slider:before {
    position: absolute;
    content: "";
    height: 1.4em;
    width: 1.4em;
    border-radius: 8px;
    left: 0.3em;
    bottom: 0.3em;
    transform: rotate(270deg);
    background-color: rgb(255, 255, 255);
    transition: .4s;
  }

  .switch input:checked + .slider {
    background-color: #21cc4c;
  }

  .switch input:focus + .slider {
    box-shadow: 0 0 1px #2196F3;
  }

  .switch input:checked + .slider:before {
    transform: translateX(1.5em);
  }
</style>
